home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
simula
/
books
/
books.lha
/
kirkerud
/
examset.sim
< prev
next >
Wrap
Text File
|
1993-08-16
|
16KB
|
507 lines
% ****************************************************************
% * *
% * This is a revised version of the program constructed in *
% * section 14.7 of *
% * Object Oriented Programming with Simula by Bj|rn Kirkerud; *
% * The revisions in the program consist in the use of *
% * a few classes containing generally useful tools *
% * *
% ****************************************************************
external class Settools;
settools begin
% ****************************************************************
% * *
% * Declarations of classes containing generally usefull tools: *
% * *
% ****************************************************************
external class promptools, texttools;
ref(promptools) pt;
ref(texttools) tt;
procedure outline(line); text line;
begin outtext(line); outimage end;
% ****************************************************************
% * *
% * The class Student: *
% * *
% ****************************************************************
element class Student;
begin
% Declarations of identifying data attribute:
text ident;
text procedure key;
key :- ident;
% Declarations of other data attributes:
! Dropped here;
% Declarations of two sets:
ref(ordered_set) courses, ! Contains references to all courses followed
! by this Student;
exams; ! Contains references to all exams taken by
! this Student;
% Declarations of data access procedures:
procedure display;
begin ref(Course) c; ref(Exam) e;
outtext(" Student " & key & " follows ");
if courses.size = 0
then outtext("no course.")
else begin
outint(courses.size, 0); outtext(" course(s): ");
c :- courses.first_element;
while c =/= none do
begin outtext(c.key & ", "); c :- courses.next_element end;
end;
outimage;
outtext(" He/she has exams in ");
if exams.size = 0
then outtext("no courses.")
else begin
outtext("the following "); outint(exams.size, 0);
outtext(" course(s): ");
e :- exams.first_element;
while e =/= none do
begin
outimage;
outtext(" " & e.course_code & ", grade: " & e.grade);
e :- exams.next_element;
end;
end;
outimage;
end of display;
procedure read;
begin
! Imperatives that prompt for and read data-attributes
! of this Student. No such imperatives are necesseray
! in this version because all data-attributes (except ident)
! have been dropped;
end;
procedure put_to_file(stud_file); ref(outfile) stud_file;
inspect stud_file do
begin outtext(ident); outimage end;
procedure get_from_file(stud_file); ref(infile) stud_file;
inspect stud_file do
begin inimage; ident :- copy(image.strip) end;
% Initialization of the data structure of this Student:
courses :- new ordered_set;
exams :- new ordered_set;
end of Student;
element class Course;
begin
% Declarations of identifying data attribute:
text code;
text procedure key;
key :- code;
% Declarations of other data attributes:
! Dropped here;
% Declarations of two sets:
ref(ordered_set) students, ! Contains references to all students who
! follow this Course;
exams; ! Contains references to all exams taken
! in this Course;
% Declarations of data access procedures:
procedure display;
begin ref(Student) s;
outtext(" Course " & key & " is followed by ");
if students.size = 0 then outtext("no students.")
else begin
outint(students.size, 0); outtext(" student(s): ");
s :- students.first_element;
while s =/= none do
begin outtext(s.key & ", "); s :- students.next_element end;
end;
outimage;
end of display;
procedure read;
begin ! Not necessary in this simplified version; end;
procedure put_to_file(stud_file); ref(outfile) stud_file;
inspect stud_file do
begin ref(Student) s; ref(Exam) e;
outtext(code); outimage;
outint(students.size, 0); outimage;
s :- students.first_element;
while s =/= none do
begin
outtext(s.ident); outimage;
s :- students.next_element;
end;
outint(exams.size, 0); outimage;
e :- exams.first_element;
while e =/= none do
begin
e.put_to_file(stud_file);
e :- exams.next_element;
end;
end;
procedure get_from_file(stud_file); ref(infile) stud_file;
inspect stud_file do
begin
ref(Student) s; ref(Exam) e;
integer nstudents, studentn, nexams, examn;
inimage; code :- copy(image.strip);
inimage; nstudents := inint;
for studentn := 1 step 1 until nstudents do
begin
inimage;
s :- all_students.find_element(copy(image.strip));
if s =/= none then
begin
students.add_element_ok(s);
s.courses.add_element_ok(this Course);
end;
end;
inimage; nexams := inint;
for examn := 1 step 1 until nexams do
begin
e :- new Exam; e.get_from_file(this infile);
all_exams.add_element_ok(e);
exams.add_element_ok(e);
s :- all_students.find_element(e.student_ident);
if s =/= none then s.exams.add_element_ok(e);
end;
end;
% Initialization of the local data structure of this Course: ;
students :- new ordered_set;
exams :- new ordered_set;
end of Course;
element class Exam;
begin
text course_code, student_ident, grade;
text procedure key; key :- course_code & "!127!" & student_ident;
procedure display;
begin
outtext(" Exam in course " & course_code &
" by student " & student_ident &
", grade: " & grade);
outimage;
end;
procedure put_to_file(stud_file); ref(outfile) stud_file;
inspect stud_file do
begin
outtext(course_code); outimage;
outtext(student_ident); outimage;
outtext(grade); outimage;
end;
procedure get_from_file(stud_file); ref(infile) stud_file;
inspect stud_file do
begin
inimage; course_code :- copy(image.strip);
inimage; student_ident :- copy(image.strip);
inimage; grade :- copy(image.strip);
end;
end of Exam;
ref(ordered_set) all_students, ! A set of all students;
all_courses, ! A set of all courses;
all_exams; ! A set of all exams;
% Declarations of command procedures: ;
procedure Give_help;
begin
outline("The legal commands are: ");
outline(" ?: Help (writes this text)");
outline(" s: To enter data about a new student");
outline(" c: To enter data about a new course");
outline(" j: Student joins a course");
outline(" sq: Student quits a course");
outline(" e: Student takes exam in a course");
outline(" ds: Displays data about a specified student");
outline(" dc: Displays data about a specified course");
outline(" das: Displays all students");
outline(" dac: Displays all courses");
outline(" dae: Displays all exams");
outline(" pf: Puts all data to a file");
outline(" gf: Gets all data from a file");
outline(" q: Quit (the program execution stops)");
end of Give_help;
procedure Enter_student;
begin ref(Student) a_student;
a_student :- new Student;
a_student.ident :- pt.ask_for_text("Student ident? ");
if all_students.add_element_ok(a_student)
then a_student.read
else outline("This student has been entered before!");
end of Enter_student;
procedure Enter_course;
begin ref(Course) a_course;
a_course :- new Course;
a_course.code :- pt.ask_for_text("Course code? ");
if all_courses.add_element_ok(a_course)
then a_course.read
else outline("This course has been entered before!");
end of Enter_course;
procedure Join_course;
begin
ref(Student) s; ref(Course) c;
s :- all_students.find_element(pt.ask_for_text( "Student ident? "));
if s == none
then outline( "No student with that ident!" )
else begin
c :- all_courses.find_element(pt.ask_for_text( "Course code? "));
if c == none
then outline( "No course with that code!")
else begin
s.courses.add_element_ok(c);
c.students.add_element_ok(s);
outline("Student " & s.key & " has been accepted in course "
& c.key);
end;
end;
end of Join_course;
procedure Quit_course;
begin
ref(Course) c; ref(Student) s;
s :- all_students.find_element(pt.ask_for_text( "Student ident? "));
if s == none
then outline( "No student with that ident!" )
else begin
c :- all_courses.find_element(pt.ask_for_text( "Course code? "));
if c == none
then outline( "No course with that code!")
else begin
if s.courses.remove_element_ok(c.key)
then begin
c.students.remove_element_ok(s.key);
outline("Student " & s.key & " no longer follows course "
& c.key);
end
else outline("The student did not follow that course!");
end of c =/= none;
end of s =/= none;
end of Quit_course;
procedure Take_exam;
begin
ref(Student) s; ref(Course) c; ref(Exam) e;
c :- all_courses.find_element(pt.ask_for_text( "Course code? "));
if c == none
then outline( "No course with that code")
else begin
s :- all_students.find_element(pt.ask_for_text( "Student ident? "));
if s == none
then outline( "No student with that ident!")
else begin
e :- new Exam;
e.course_code :- c.code; e.student_ident :- s.ident;
e.grade :- pt.ask_for_text("Grade? ");
s.exams.add_element_ok(e);
c.exams.add_element_ok(e);
all_exams.add_element_ok(e);
s.courses.remove_element_ok(c.key);
c.students.remove_element_ok(s.key);
outline("Student " & s.key & " is recorded to have taken " &
"exam in course " & c.key);
end of s =/= none;
end of c =/= none;
end of Take_exam;
procedure Display_student;
begin ref(Student) s;
s :- all_students.find_element(pt.ask_for_text( "Student ident? "));
if s == none
then outline( "No student with that ident!")
else s.display;
outimage;
end;
procedure Display_course;
begin ref(Course) c;
c :- all_courses.find_element(pt.ask_for_text( "Course ident? "));
if c == none
then outline( "No course with that ident!")
else c.display;
outimage;
end;
procedure Display_all_students;
begin ref(Student) s;
outline("The students:");
s :- all_students.first_element;
while s =/= none do
begin s.display; s :- all_students.next_element end;
outimage;
end;
procedure Display_all_courses;
begin ref(Course) c;
outline("The courses:");
c :- all_courses.first_element;
while c =/= none do
begin c.display; c :- all_courses.next_element end;
outimage;
end;
procedure Display_all_exams;
begin ref(Exam) e;
outline("The exams:");
e :- all_exams.first_element;
while e =/= none do
begin e.display; e :- all_exams.next_element end;
outimage;
end;
procedure Put_to_file;
inspect new outfile(pt.ask_for_text("File name? ")) do
begin
ref(Student) s; ref(Course) c; ref(Exam) e;
open(blanks(100));
outint(all_students.size, 0); outimage;
s :- all_students.first_element;
while s =/= none do
begin s.put_to_file(this outfile); s :- all_students.next_element end;
outint(all_courses.size, 0); outimage;
c :- all_courses.first_element;
while c =/= none do
begin c.put_to_file(this outfile); c :- all_courses.next_element end;
close;
end Put_to_file;
procedure Get_from_file;
inspect new infile(pt.ask_for_text("File name? ")) do
begin
ref(Student) s; ref(Course) c; ref(Exam) e;
integer nstudents, studentn, ncourses, coursen;
if open(blanks(100))
then begin
inimage; nstudents := inint;
for studentn := 1 step 1 until nstudents do
begin
s :- new student;
s.get_from_file(this infile);
all_students.add_element_ok(s);
end;
inimage; ncourses := inint;
for coursen := 1 step 1 until ncourses do
begin
c :- new course;
c.get_from_file(this infile);
all_courses.add_element_ok(c);
end;
close;
end
else outline("The file & filename & can not be opened for reading!");
end Get_from_file;
procedure Read_and_execute_commands;
begin text command;
Give_help;
command :- pt.ask_for_text("Enter command> ");
while command ne "q" do
begin
if command = "?" then Give_help else
if command = "s" then Enter_student else
if command = "c" then Enter_course else
if command = "j" then Join_course else
if command = "sq" then Quit_course else
if command = "e" then Take_exam else
if command = "ds" then Display_student else
if command = "dc" then Display_course else
if command = "das" then Display_all_students else
if command = "dac" then Display_all_courses else
if command = "dae" then Display_all_exams else
if command = "pf" then Put_to_file else
if command = "gf" then Get_from_file
else begin
outline("Unknown command: " & command);
Give_help;
end;
command :- pt.ask_for_text("Enter command> ");
end;
end of Read_and_execute_commands;
procedure Unknown_command(c); character c;
begin
outline(" You gave the command '" & tt.char_as_text(c) & "'" &
" This is not among the legal commands.");
outline(" Type ? if you don't remember the legal commands");
end of Unknown_command;
procedure not_implemented(action_name); text action_name;
begin
outline(" You gave the command """ & action_name &
""". This has not been implemented yet!");
outline(" Try another command!");
end;
pt :- new promptools;
tt :- new texttools;
all_students :- new ordered_set;
all_courses :- new ordered_set;
all_exams :- new ordered_set;
Read_and_execute_commands;
if pt.ask_for_bool("Do you want to put all data to file? ")
then Put_to_file;
outline("Bye");
end